package admin

import (
	"github.com/gin-gonic/gin"
	"wonlicsys/database"
	"wonlicsys/pkg/dingding"
	"wonlicsys/service/adminuser"
	"wonlicsys/utils/common"
)

func Login(c *gin.Context) {
	reqParam := adminuser.ReqAdminUserLogin{}
	if err := common.GetParamStruct(c, &reqParam); err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	//数据库
	db, err := database.GetDbWonLicGorm()
	if err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	defer database.CloseDbWonLicGorm(db)
	var rspService adminuser.AdminService
	if err := rspService.Login(db, &reqParam); err != nil {
		common.JsonResult(c, 200, "success", err.Error())
		return
	}
	common.JsonResult(c, 200, "success", rspService.LogService.JwtToken)
}

func Info(c *gin.Context) {
	userId := c.MustGet("userName")
	common.JsonResult(c, 200, "success", userId)
}

func Test(c *gin.Context) {

	dinService := dingding.GetInstance()
	res, err := dinService.GetByMobile("15257175937")
	if err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}

	//err := database.RdbSet("abc", "测试redis连接池aaa")
	//if err != nil {
	//	fmt.Println(err)
	//	return
	//}
	//res, err := database.RdbGet("abc")
	//if err != nil {
	//	fmt.Println("get abc faild :", err)
	//	return
	//}
	common.JsonResult(c, 200, "success", res)
}
